<template>
  <div class="wrapper">
    <div class="header">
      <div class="header-item"><i class="el-icon-s-order"></i>我的订单</div>
    </div>
    <div class="content-box">
      <!-- 没有数据 -->
      <div class="imgss" v-if="orderslist.length === 0">
        <img src="../assets/imgs/cart-empty.png" alt="" />
        <div class="right">
          <h2>您的订单还是空的！</h2>
          <p>快去购物吧!</p>
        </div>
      </div>
      <div class="order-box" v-else>
        <table v-for="(item, index) in orderslist" :key="index">
          <div class="header-box">
            <p>订单编号: {{ item[0].order_id }}</p>
            <p>
              {{ item[0].order_time | dateFormat }}
            </p>
          </div>
          <tr>
            <th></th>
            <th>商品名称</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
          </tr>
          <tr v-for="it in item" :key="it.id">
            <td>
              <img
                :src="'http://101.132.181.9:3000/' + it.product_picture"
                alt=""
                @click="tiao(it.product_id)"
              />
            </td>
            <td @click="tiao(it.product_id)">{{ it.product_name }}</td>
            <td>{{ it.product_price }}</td>
            <td>{{ it.product_num }}</td>
            <td style="color: #ff6700">
              {{ it.product_price * it.product_num }}
            </td>
          </tr>
          <tr>
            <td>
              共
              <span style="color: #ff6700; font-size: 18px">{{
                orderslist[index].length
              }}</span>
              件商品
            </td>
            <td></td>
            <td></td>
            <td></td>
            <td style="color: #ff6700">
              合计:
              <span style="font-size: 25px">{{ priceAll.checkPrice }}</span>
            </td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
import { OrderAll } from "@/api/orders";
export default {
  name: "order",
  data() {
    return {
      orderslist: [],
      userId: this.$store.state.users.user_id,
    };
  },
  created() {
    this.getOrderList();
  },
  computed: {
    priceAll() {
      var checkPrice = 0;
      var allNum = 0;
      this.orderslist.filter(function (item) {
        console.log(item, "item");
        item.forEach((key) => {
          console.log(key, "key");
          if (key.order_id) {
            allNum += key.product_num;
            checkPrice += key.product_num * key.product_price;
          }
        });
      });
      // for (const item in this.orderslist) {
      //   console.log(this.orderslist[item], "111111111111111");
      //   for (const key in this.orderslist[item]) {
      //     if (key < this.orderslist[item].length) {
      //       allNum += this.orderslist[item].product_num;
      //       checkPrice +=
      //         this.orderslist[item].product_num *
      //         this.orderslist[item].product_price;
      //     }
      //   }
      // }
      return { checkPrice, allNum };
    },
  },

  methods: {
    /**
     * 跳转详情
     */
    tiao(type) {
      this.$router.push({
        path: "/goods/details", //跳转的路由
        query: {
          //跳转路由的参数
          type: type, //前面的type是参数的名称 //后面的type是点击产品时传过来的型号 这里也就是m320
        },
      });
    },

    /**
     * 获取数据
     */
    async getOrderList() {
      const res = await OrderAll({
        user_id: this.userId,
      });
      console.log(res);
      if (
        res.data.code === "001" ||
        res.data.code == "002" ||
        res.data.code == "003"
      ) {
        this.orderslist = res.data.orders;
        console.log(this.orderslist);
        this.$notify.success({
          title: "成功",
          message: res.data.msg,
          showClose: true,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}
.wrapper {
  width: 100%;
  height: 100%;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
  align-items: center;
  .header {
    height: 64px;
    width: 100%;
    background: white;
    border-bottom: 3px solid rgb(255, 103, 0);
    display: flex;
    flex-direction: column;
    align-items: center;
    .header-item {
      i {
        font-size: 40px !important;
        color: rgb(255, 103, 0);
      }
      width: 1225px;
      margin: 0 auto;
      height: 64px;
      line-height: 64px;
      font-size: 28px !important;
    }
  }
  .content-box {
    width: 1225px;
    // height: 100px;
    height: 100%;
    margin-top: 10px;
    // 空数据
    .imgss {
      display: flex;
      img {
        height: 300px;
        width: 360px;
      }
      .right {
        margin-left: 30px;
        color: #b0b0b0;
        h2 {
          margin: 70px 0 15px;
          font-weight: 600;
          font-size: 36px !important;
        }
        p {
          margin: 0 0 20px;
          font-size: 20px;
        }
      }
    }
    .order-box {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      margin-top: 20px;

      table {
        width: 100%;
        height: 100%;
        background: white;
        display: flex;
        margin-top: 50px;
        font-size: 16px !important;
        flex-direction: column;
        .header-box {
          height: 60px;
          padding: 0 26px;
          color: #424242;
          font-size: 16px !important;
          border-bottom: 1px solid #ff6700;
          display: flex;
          justify-content: space-between;
          align-items: center;
          :nth-child(1) {
            color: #ff6700;
          }
        }
        tr {
          width: 1225px;
          height: 85px;
          color: #424242;
          border-bottom: 1px solid #e0e0e0;
          display: flex;
          align-items: center;
          justify-content: space-between;
          th,
          td {
            width: 25%;
            height: 85px;
            display: flex;
            justify-content: center;
            align-items: center;
          }
          img {
            height: 80px;
            width: 80px;
          }
        }
      }
    }
  }
}
</style>